﻿const TOKEN=localStorage.getItem('token');

//开发
export const PAY_IP="http://192.168.0.119:8081";
export const HREF=''
const IP='http://192.168.0.119:8081';

// 生产
// const IP='';
// export const HREF='';
// export const PAY_IP="";


//弹框方法
export const _layer=function(content){
    layer.open({ 
      content: content,
      skin: "msg",
      time: 3, 
      shift:1,  
      shadeClose:true   
    });
}



//验证手机号正则
export const _checkMobile=function(sMobile){ 
  let str=String(sMobile).trim(); 
  if(str.length==11&&str.slice(0,1)=='1'){
    return true;
  }else{
    _layer('请输入正确的手机号');
    return false; 
  }
}


//转换时间戳
export const _date=function(timestamp){
  if(!timestamp){
    return '';
  }
  var date;
  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  if(String(timestamp).length==10){
    date=new Date(Number(timestamp) * 1000);
  }
  if(String(timestamp).length==13){
    date=new Date(Number(timestamp));
  }
      let Y = date.getFullYear() + '-';
      let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
      let D = date.getDate() + ' ';
      let h = date.getHours() + ':';
      let m = date.getMinutes() + ':';
      let s = date.getSeconds();
      return Y+M+D+h+m+s;
}


//钱分转换为元
export const _money=function(val){
    let judge=String(val);
    if(judge=='undefined'||judge=='null'){
      return 0.00;
    }
    let priceStr=String(parseFloat(parseInt(val)/100));
    let index=priceStr.indexOf('.');
    if(index!=-1){
        let handle=priceStr.slice(index+1);
        if(handle.length==1){
            priceStr=priceStr+'0'
        }
    }else{
        priceStr=priceStr+'.00';
    }
    return priceStr;
}

//钱元转为分
export const _fen=function(val){
    let str=String(val);
    let point=str.indexOf('.');
    if(point!=-1){
        let zhenshu=parseInt(str.slice(0,point))*100;
        let pointAfter=str.slice(point+1);
        if(pointAfter.length==1){
            return zhenshu+parseInt(pointAfter)*10;
        }else if(pointAfter.length>=2){
            return zhenshu+parseInt(pointAfter.slice(0,1))*10+parseInt(pointAfter.slice(1,2));
        }else{
            return zhenshu;
        }
    }else{
        return parseFloat(val)*100;
    }
}

//得到参数
export const _gethash=function(val){
    // val样板:window.location.href.slice(window.location.href.indexOf('?'))
    // 返回：
    // {
    //   a:"jhgdsjkfasdfa"
    //   data:"%5B%7B%22goodsNum%22%3A%22191942513806708736%22%2C%22count%22%3A1%2C%22specNum%22%3A%22191942514490380289%22%7D%5D"
    // }
    var arr = val.replace(/^\?/,'').split("&");
    var params = {};
    for(var i=0; i<arr.length; i++){
        var data = arr[i].split("=");
        if(data.length == 2){
            params[data[0]] = data[1];
        }
    }
    return params;
}

export const AJAX=function(url,type,data,cal,error){
	let req=JSON.stringify(data) !== "{}"?JSON.stringify(data):'';
	$.ajax({
		  url:url,
	    dataType:'json', 
      headers:{ 
          Accept:"application/json", 
          "Content-Type":"application/json",
          token:TOKEN
      }, 
      cache:false,
      processData:false,
	    type: type,
	    data:req,
	    success: function (data) {
        if(String(data.code)== '-1'){
            openHtml();
            return ;
        }
        ready=true;
	    	cal(data);
	    },
	    error:function(){
	    	if(typeof error == 'function'){
	    		error();
	    	}else{
	    		_layer('服务器开小差了');
	    	}
	    }
	});
}

let ready=true;
function openHtml(){
    if(ready){
      window.location.href=HREF+'/login.html';
      ready=false;
    }
}

export const GET_AJAX=function(url,data,cal){
    $.ajax({
      url:url,
      headers:{ 
        token:TOKEN
      }, 
      type: 'get',
      data:data,
      success: function (data) {
        if(String(data.code)== '-1'){
            openHtml();
            return ;
        }
        ready=true;
        cal(data);
      },
      error:function(){
          _layer('服务器开小差了');
      }
    });
}

    

    // ============ 判断json ==============


function isObj(object) {
    return object && typeof(object) == 'object' && Object.prototype.toString.call(object).toLowerCase() == "[object object]";
}

function isArray(object) {
    return object && typeof(object) == 'object' && object.constructor == Array;
}

function getLength(object) {
    var count = 0;
    for(var i in object) count++;
    return count;
}

function CompareObj(objA, objB, flag) {
    for(var key in objA) {
        if(!flag) //跳出整个循环
            break;
        if(!objB.hasOwnProperty(key)) {
            flag = false;
            break;
        }
        if(!isArray(objA[key])) { //子级不是数组时,比较属性值
            if(objB[key] != objA[key]) {
                flag = false;
                break;
            }
        } else {
            if(!isArray(objB[key])) {
                flag = false;
                break;
            }
            var oA = objA[key],
                oB = objB[key];
            if(oA.length != oB.length) {
                flag = false;
                break;
            }
            for(var k in oA) {
                if(!flag) //这里跳出循环是为了不让递归继续
                    break;
                flag = CompareObj(oA[k], oB[k], flag);
            }
        }
    }
    return flag;
}




export const _jsonEqual=function(objA, objB) {
    if(!isObj(objA) || !isObj(objB)) return false; //判断类型是否正确
    if(getLength(objA) != getLength(objB)) return false; //判断长度是否一致
    return CompareObj(objA, objB, true); //默认为true
}



    // ============ 判断数组 ==============
export const _arrEqual=function (arr1,arr2) {
  
  if (arr1.length != arr2.length)
    return false;
  for (var i = 0, l = arr1.length; i < l; i++) {
    // Check if we have nested arrays
    if (arr1[i] instanceof Array && arr2[i] instanceof Array) {
      // recurse into the nested arrays
      if (!_arrEqual(arr1[i],arr2[i]))
        return false;    
    }      
    else if (arr1[i] != arr2[i]) { 
      return false;  
    }      
  }    
  return true;
}


		// ============= 优惠卷 ===============

//用户所有的优惠卷
export const MY_COUPON=IP+'/user/coupon/all';


        // ============= 我的 =================

//我的二维码-刘畅
export const MY_QR=IP+'/user/qr';

//我的邀请--刘畅
export const MY_INVITATION=IP+'/user/findInvitorByUserNum';

//增加收货地址-非鸿
export const ADD_ADDRESS=IP+'/user/addr/saveAddress';

//获取用户收获地址列表-非鸿
export const GET_ADDRESS=IP+'/user/addr/getAddress';

//用户选择收货地址-非鸿
export const CHANGE_ADDRESS=IP+'/user/addr/chooseAddress';

//个人信息-胡秀琴
export const MY_INFO=IP+'/user';

//我的趣币-胡秀琴
export const MY_QUMONEY=IP+'/user/integral/log';

//支出收入展示
export const CONSUMPTION_LIST=IP+'/user/log';

//我的1-刘畅  是待付款数量之类接口
export const ALL_COUNT=IP+'/user/showDetail';




        // ============ 登陆 ===================

//获取验证码
export const SEND_CODE=IP+'/login/getPhoneCode';

//登陆
export const LOGIN=IP+'/login';





        // ============= 首页 ===================

//搜索历史
export const GOODS_SEARCH=IP+'/index/search/history';

//执行搜索
export const EXECUTE_SEARCH=IP+'/index/search';

//分类数据
export const INDEX_TYPE=IP+'/index/type';  

//根据分类拉取数据
export const GET_GOODS_BY_TYPE=IP+'/goods/list';

        // ============= 用户订单部分 ===========

//用户预提交订单-郭静
export const RADIY_ORDER=IP+'/order/beforehand';

//用户下订单-胡秀琴
export const BUILD_ORDER=IP+'/order/createOrder';

//用户实名认证接口
export const USER_SHIMING=IP+'/user/realNameIdCard';

//退货退款
export const REFUND=IP+'/order/return/goods';

//订单详情
export const ORDER_IFNO=IP+'/order/orderDetail';

//我的订单
export const MY_ORDER_LIST=IP+'/order/orders';

//订单支付
export const ORDER_PAY=IP+'pay/';


        // ============= 用户资金转账部分 ===========

//用户提现
export const USER_GET_MONEY=IP+'/user/money/withdraw';   


        // ============= 购物车部分 ===========

//加入商品到购物车
export const ADD_IN_CAR=IP+'/user/shopCar/saveGoodsCar';

//购物车列表
export const SHOPPING_CAR=IP+'/user/shopCar';

//删除商品
export const DELETE_SHOPPING=IP+'/user/shopCar/delete';

        

        // ============= 商品部分 ===========
//主页
export const INDEX=IP+'/index/setting';

//type==2的时候去调用户个人信息
export const TYPE2_USER=IP+'/user/user';

//type==5的时候去调商品的信息
export const TYPE5_GOODS_INFO=IP+'index/goods?num=';

//商品详情
export const GOODS_INFO=IP+'/goods/'

//选择规格
export const CHANGE_GUIGE=IP+'/goods/specifications';

//商品评价
export const GOODS_COMMON=IP+'/evaluate/comments/';


        // ============= 订单评价 ===========

//拉取订单评价的信息
export const ORDER_COMMON_INFO=IP+'/evaluate/';

//评价订单
export const ORDER_GO_COMMON=IP+'/order/evaluate';

//查看订单物流
export const LIKE_ORDER_WULIUL=IP+'/order/selectExpressage/' 

//订单取消
export const ORDER_CLOSE=IP+'/order/cancelOrder/'

//订单删除
export const ORDER_DEL=IP+'/order/delete/';

//确认收货
export const SUER_GET_GOODS=IP+'/order/autoConfirmed/';

//提醒发货
export const NOTICE_FAHUO=IP+'/order/remind/';




